// --------------------------------------------------------------------
//   name 	: 
//   desc 	: 
//   version 	: 
//   dev 	: 
// --------------------------------------------------------------------
// DEPENDENCIES
// 
// --------------------------------------------------------------------
// CHANGE HISTORY
// 0.1 	Initial Release
// --------------------------------------------------------------------
	plugins.pluginManager = { // Add plugin information for a user to see
		'info' : {
			'mandatory' : true,
			'name' : "Plugin Manager",
			'desc' : "The plugin manager helps you enable and disable which plugins to use and not to use, now also allowing you to change plugin specific settings, totally customizing your TWE to your own preferences.",
			'version' : "0.1",
			'dev' : "Timas, Mog & Jelian"
			},


// --------------------------------------------------------------------
// Function : 
// --------------------------------------------------------------------
  'enhance_game_settings' : function() {

	  var lhtml = '<tr><td>&nbsp;</td></tr><tr><td id="twe_settings"><a href="javascript:showSettings();">'+twe.lang("setting_show")+'</a></td></tr>'
		+'<tr><td id="plugin_settings"><a href="javascript:showPluginSettings();">TWE Plugin Settings</a></td></tr>'
		+'<tr><td id="plugin_prefs" nowrap="nowrap" style="width:180px;"><a href="javascript:showPreferences();">TWE Preferences</a></td></tr>';

	  $("table.main table table.vis:first").append(lhtml);

	  setFunc("switchSetting", function(name) { 
		  window.setTimeout(function() {
			  twe.setPlugin(name);
		  }, 0); 
	  });

	  setFunc("info", function(name) {

		  $("#info_"+name).slideToggle("slow");
	  });
	  
	  setFunc("setPluginVariable", function(type,varname,currentItem) {
		window.setTimeout(function() {
			if(type=="boolean") {
				isChecked = (currentItem.checked)?true:false;
				setValuePlugin(varname,isChecked);
			} else {
				setValuePlugin(varname,currentItem.value);
			}
		}, 0);
	  });

	setFunc("savePrefs", function() { 

		window.setTimeout(function() {
			prefs = eval(document.getElementById('prefBox').value);
	
			for (i in prefs) {
				setValue(i, eval(prefs[i]));
			}
		}, 0);

		alert(twe.lang('settings_prefSaved'));

	});

	setFunc("showPreferences", function() {

		$("table.main table.vis:first .selected").removeClass("selected");
		$("#plugin_prefs").addClass("selected");

		window.setTimeout(function() { 

			var data = getValue("storedPrefs");
			prefObj = {};

			for (i in data) {
				prefObj[i] = uneval( getValue(i) );
			}

 			var html = "<table class=\"plugins vis\" valign=\"top\"><tr><th class=\"smallsize\" colspan=\"2\">"+twe.lang("setting_preferences")+"</th></tr>";
			html += "<tr><td colspan=\"2\"><textarea id=\"prefBox\" rows=\"25\" cols=\"55\">"+uneval(prefObj)+"</textarea></td></tr>";
			html += "<tr><td align=\"center\"><input type=\"button\" name=\"twe_storeprefs\" value=\""+twe.lang('settings_prefSelect')+"\" onclick=\"document.getElementById('prefBox').focus();document.getElementById('prefBox').select();\" /></td><td align=\"center\">";
			html += "<input type=\"button\" name=\"twe_storeprefs\" value=\""+twe.lang('settings_prefSave')+"\" onclick=\"savePrefs();\" /></td></tr>";
			html += "</table>";

			html = '<table><tr><td valign="top"><table class="vis">'+$("table.main table.vis:first").html()+'</table></td><td>'+html+'</td></tr></table>';

			$("table.main table:first").before(html).remove();

		}, 0);
	});

	  setFunc("showSettings", function() {
		  window.setTimeout(function() {

			  var html = "<table class=\"plugins vis\"><tr><th class=\"smallsize\">"+twe.lang("setting_toggle")+"</th><th>"+twe.lang("setting_name")+"</th></tr>";

			  var plugs = twe.getPlugins();
			  for(var p in plugins) {

				  var pid = plugins[p];
				  if (typeof pid.info != 'object') pid.info = {};
				  if (new String(pid.info.name) == 'undefined') pid.info.name = p;
				  if (new String(pid.info.desc) == 'undefined') pid.info.desc = 'N/A';
				  if (new String(pid.info.version) == 'undefined') pid.info.version = 'N/A';
				  if (new String(pid.info.dev) == 'undefined') pid.info.dev = 'N/A';
				  if (new String(pid.info.mandatory) == 'undefined') pid.info.mandatory = false;

				  if (plugs.indexOf(p) != -1) {
					  var status = '';
				  } else {
					  var status = 'checked="true"';
				  }

				  if (pid.info.mandatory == true) {
					  var mand = ' disabled="disabled"';
				  } else {
					  var mand = '';
				  }
				  
				  /* Cycle through variables and make the variable setting output */
				  var VariableHTML = "";
				  var PluginVariableData = pid.variables;
				  if(PluginVariableData) {
				  var VarCount = 0;
					for(var CurrentVariable in PluginVariableData) {
						if(new String(CurrentVariable)=="tooltip") continue;
						if(new String(CurrentVariable)=="desc") continue;
						var hasToolTip = (PluginVariableData["tooltip"])?true:false;
						var hasVarToolTip = false;
						if(hasToolTip) {
							hasVarToolTip = (PluginVariableData["tooltip"][VarCount])?true:false;
							if(hasVarToolTip) {
								if(PluginVariableData["tooltip"][VarCount].length<3) {
									hasVarToolTip=false;
								}
							}
						}
						var CurrentTooltip = (hasVarToolTip)?"<a style='color:red;' href='#' title='"+new String(PluginVariableData["tooltip"][VarCount])+"'>*</a>":"";
						var hasDesc = (PluginVariableData["desc"])?true:false;
						var hasVarDesc = false;
						if(hasDesc) {
							hasVarDesc = (PluginVariableData["desc"][VarCount])?true:false;
							if(hasVarDesc) {
								if(PluginVariableData["desc"][VarCount].length<3) {
									hasVarDesc=false;
								}
							}
						}
						var CurrentDesc = (hasVarDesc)?"<span style='font-size:10px;font-family:Arial;'>"+new String(PluginVariableData["desc"][VarCount])+"</span>":"";
						VarCount++;
						CurrentName = new String(CurrentVariable);
						OriginalValue = PluginVariableData[CurrentVariable];
						CurrentValue = getValuePlugin(CurrentName);
						if(new String(CurrentValue)=='true' || new String(CurrentValue)=='false') {
							if(new String(CurrentValue)=="true") {
								var CheckSelected = "checked='true'";
							} else {
								var CheckSelected = "";
							}
							VariableHTML += "<span style='margin:0px;padding:0px;margin-left:17px;'><input onclick='javascript:setPluginVariable(\"boolean\",\""+CurrentName+"\",this);' type='checkbox' name='bol_"+CurrentName+"' "+CheckSelected+" id='bol_"+CurrentName+"'>"+CurrentTooltip+" : <label for='bol_"+CurrentName+"' style='text-decoration:none;font-weight:normal;color:#666;cursor:pointer;'>"+CurrentName+"</label></span>";
						} else if(OriginalValue.constructor.toString().indexOf("Array") != -1) { //Check if the value is an array;
							var SelectBoxHTML = "";
							for(a=0;a<OriginalValue.length;a++) {
								var isSelected = "";
								if(OriginalValue[a]==CurrentValue){
									isSelected = " selected";
								}
								SelectBoxHTML += "<option value='"+OriginalValue[a]+"'"+isSelected+">"+OriginalValue[a]+"</option>";
							}
							VariableHTML += "<span style='margin:0px;padding:0px;margin-left:17px;'><label for='sel_"+CurrentName+"' style='text-decoration:none;font-weight:normal;color:#666;cursor:pointer;'>"+CurrentName+"</label> : <select onchange='javascript:setPluginVariable(\"array\",\""+CurrentName+"\",this);' name='arr_"+CurrentName+"' id='arr_"+CurrentName+"' style='height:16;'>"+SelectBoxHTML+"</select>"+CurrentTooltip+"</span>";
						} else {
							var BoxWidth = CurrentValue.length
							if(BoxWidth>20) BoxWidth=20;
							VariableHTML += "<span style='margin:0px;padding:0px;margin-left:17px;'><label for='txt_"+CurrentName+"' style='text-decoration:none;font-weight:normal;color:#666;cursor:pointer;'>"+CurrentName+"</label> : <input onchange='javascript:setPluginVariable(\"text\",\""+CurrentName+"\",this);' type='text' name='txt_"+CurrentName+"' id='txt_"+CurrentName+"' value='"+CurrentValue+"' size='"+BoxWidth+"' style='width:"+(BoxWidth)+";font-family:monospace;height:16;'>"+CurrentTooltip+"</span>";
						}
						VariableHTML += CurrentDesc;
					}
				  }
				  
				  /*
				  How to use the global value settings for plugins
				 Add this to the plugin after the 'info':{   },
				  'variables': {
					'tooltip':new Array("ABoolean is just a test to see if this works","ASelection is a select box","ATextArea is to text box"),
					'ABoolean':true,
					'ASelection':["value1","value2","value3","value4"],
					'ATextArea':"oh my god"
				}
				
				Then in the section wherever. Use getValuePlugin("variablename") to retrieve the stored value
				  */
				  /* End of variables cycler */
				  html += "<tr><td class=\"smallsize\"><input type=\"checkbox\" name=\"set_\" "+status+" onclick=\"switchSetting('"+p+"');\" "+mand+"/></td>";
				  html += "<td style='padding:0;margin:0;padding-left:2px;padding-right:2px;'><h4><a href=\"javascript:info('"+p+"');\">"+pid.info.name+"</a></h4>";
				  html += "<div id=\"info_"+p+"\" style=\"display:none\">";
				  html += "   <span class=\"desc\"><label>"+twe.lang("setting_label_desc")+":</label> "+pid.info.desc+"</span>";
				  html += "   <span class=\"version\"><label>"+twe.lang("setting_label_version")+":</label> "+pid.info.version+"</span>";
				  html += "   <span class=\"dev\"><label>"+twe.lang("setting_label_dev")+":</label> "+pid.info.dev+"</span>";
				  if(VariableHTML!="") html += "   <span class=\"dev\"><label>Plugin Settings/Variables:</label> "+VariableHTML+"</span>";
				  html += "   <span class=\"exec\"><label>"+twe.lang("setting_label_exec")+":</label><ul>";
				  for(var method in plugins[p]) {
					  if (/enhance/.test(method)) {
						  if (/enhance_([^_]+)_(.+)/.test(method)) {
							  method = method.replace(/enhance_([^_]+)_(.+)/,"$1 : $2");
						  } else {
							  method = method.replace(/enhance_(.+)/,"$1 : All");
						  }
						  html += "<li>"+method+"</li>";
					  }
				  }
				  html += "</ul></span></div></td></tr>";
			  }
			  html += "</table>";

			  html = '<table><tr><td valign="top"><table class="vis">'+$("table.main table.vis:first").html()+'</table></td><td>'+html+'</td></tr></table>';
			  $("table.main table:first").before(html).remove();
			  $("table.main table.vis:first .selected").removeClass("selected");
			  $("#twe_settings").addClass("selected");
			  
			  
			  //$("table.main table.vis:gt(0)").remove();
			  //$("table.main p:not(:last)").remove();
			  //$("table.main table table.vis:gt(1)").remove();
			  //$("table.main table:first td:last *").remove();
			  //$("table.main table:first td:eq(2)").html(html);
		  }, 0);
	  });
	  
	  
	  	  setFunc("showPluginSettings", function() {
		  window.setTimeout(function() {

			  var html = "<table class=\"plugins vis\"><tr><th>Plugin Settings Manager</th></tr>";

			  var plugs = twe.getPlugins();
			  for(var p in plugins) {

				  var pid = plugins[p];
				  if (typeof pid.info != 'object') pid.info = {};
				  if (new String(pid.info.name) == 'undefined') pid.info.name = p;
				  
				  /* Cycle through variables and make the variable setting output */
				  var VariableHTML = "";
				  var PluginVariableData = pid.variables;
				  if(PluginVariableData) {
				  var VarCount = 0;
					for(var CurrentVariable in PluginVariableData) {
						if(new String(CurrentVariable)=="tooltip") continue;
						if(new String(CurrentVariable)=="desc") continue;
						var hasToolTip = (PluginVariableData["tooltip"])?true:false;
						var hasVarToolTip = false;
						if(hasToolTip) {
							hasVarToolTip = (PluginVariableData["tooltip"][VarCount])?true:false;
							if(hasVarToolTip) {
								if(PluginVariableData["tooltip"][VarCount].length<3) {
									hasVarToolTip=false;
								}
							}
						}
						var CurrentTooltip = (hasVarToolTip)?"<a style='color:red;' href='#' title='"+new String(PluginVariableData["tooltip"][VarCount])+"'>*</a>":"";
						var hasDesc = (PluginVariableData["desc"])?true:false;
						var hasVarDesc = false;
						if(hasDesc) {
							hasVarDesc = (PluginVariableData["desc"][VarCount])?true:false;
							if(hasVarDesc) {
								if(PluginVariableData["desc"][VarCount].length<3) {
									hasVarDesc=false;
								}
							}
						}
						var CurrentDesc = (hasVarDesc)?"<span style='font-size:10px;font-family:Arial;'>"+new String(PluginVariableData["desc"][VarCount])+"</span>":"";
						VarCount++;
						CurrentName = new String(CurrentVariable);
						OriginalValue = PluginVariableData[CurrentVariable];
						CurrentValue = getValuePlugin(CurrentName);
						if (new String(CurrentValue) == 'undefined') {
							CurrentValue = false;
						}
						if(CurrentValue==true || CurrentValue==false) {
							if(new String(CurrentValue)=="true") {
								var CheckSelected = "checked='true'";
							} else {
								var CheckSelected = "";
							}
							VariableHTML += "<span style='margin:0px;padding:0px;margin-left:17px;'><input onclick='javascript:setPluginVariable(\"boolean\",\""+CurrentName+"\",this);' type='checkbox' name='bol_"+CurrentName+"' "+CheckSelected+" id='bol_"+CurrentName+"'>"+CurrentTooltip+" : <label for='bol_"+CurrentName+"' style='text-decoration:none;font-weight:normal;color:#666;cursor:pointer;'>"+CurrentName+"</label></span>";
						} else if(OriginalValue.constructor.toString().indexOf("Array") != -1) { //Check if the value is an array;
							var SelectBoxHTML = "";
							for(a=0;a<OriginalValue.length;a++) {
								var isSelected = "";
								if(OriginalValue[a]==CurrentValue){
									isSelected = " selected";
								}
								SelectBoxHTML += "<option value='"+OriginalValue[a]+"'"+isSelected+">"+OriginalValue[a]+"</option>";
							}
							VariableHTML += "<span style='margin:0px;padding:0px;margin-left:17px;'><label for='sel_"+CurrentName+"' style='text-decoration:none;font-weight:normal;color:#666;cursor:pointer;'>"+CurrentName+"</label> : <select onchange='javascript:setPluginVariable(\"array\",\""+CurrentName+"\",this);' name='arr_"+CurrentName+"' id='arr_"+CurrentName+"' style='height:16;'>"+SelectBoxHTML+"</select>"+CurrentTooltip+"</span>";
						} else {
							var BoxWidth = CurrentValue.length
							if(BoxWidth>20) BoxWidth=20;
							VariableHTML += "<span style='margin:0px;padding:0px;margin-left:17px;'><label for='txt_"+CurrentName+"' style='text-decoration:none;font-weight:normal;color:#666;cursor:pointer;'>"+CurrentName+"</label> : <input onchange='javascript:setPluginVariable(\"text\",\""+CurrentName+"\",this);' type='text' name='txt_"+CurrentName+"' id='txt_"+CurrentName+"' value='"+CurrentValue+"' size='"+BoxWidth+"' style='width:"+(BoxWidth)+";font-family:monospace;height:16;'>"+CurrentTooltip+"</span>";
						}
						VariableHTML += CurrentDesc;
					}
				  }
				  /* End of variables cycler */
				  if(VariableHTML!="") html += "<tr><td><span class=\"dev\"><label>"+pid.info.name+":</label> "+VariableHTML+"</span></td></tr>";
			  }
			  html += "</table>";

			  html = '<table><tr><td valign="top"><table class="vis">'+$("table.main table.vis:first").html()+'</table></td><td>'+html+'</td></tr></table>';
			  $("table.main table:first").before(html).remove();
			  $("table.main table.vis:first .selected").removeClass("selected");
			  $("#plugin_settings").addClass("selected");
			  
		  }, 0);
	  });
	  
  }
	};